﻿namespace CAION2.ViewModels
{
    using System.Collections.ObjectModel;
    using System.Data;
    using System.Linq;

    using Classes;
    using Commands;
    using Models;

    using FirstFloor.ModernUI.Windows.Controls;

    class RandomBonusViewModel : ViewModelBase
    {
        private RandomBonus selectedRandomBonus;
        
        public ModernDialog Dialog { private get; set; }
        public ObservableCollection<RandomBonus> RandomBonusesList { get; set; }

        public RandomBonus SelectedRandomBonus
        {
            get
            {
                return selectedRandomBonus;
            }

            set
            {
                if (Equals(value, selectedRandomBonus)) return;
                selectedRandomBonus = value;

                OnPropertyChanged("SelectedRandomBonus");
            }
        }

        public RelayCommand DeleteRandomBonusCommand { get; private set; }

        public RandomBonusViewModel()
        {
            DeleteRandomBonusCommand = new RelayCommand(DeleteRandomBonus) { IsEnabled = true };
        }

        public void LoadRandomBonusList(string name, RandomBonus currentRandomBonus)
        {
            DataTable randomTable
                = DataModel.GetTable("Random_Options", "*", string.Format("name = '{0}'", name));

            RandomBonusesList = new ObservableCollection<RandomBonus>();

            foreach (DataRow row in randomTable.Rows)
            {
                RandomBonusesList.Add(new RandomBonus(row));
            }
            OnPropertyChanged("RandomBonusesList");

            if (currentRandomBonus == null) return;
            if (!RandomBonusesList.Any()) return;
            SelectedRandomBonus = RandomBonusesList.First(r => r.Id == currentRandomBonus.Id);
        }

        private void DeleteRandomBonus()
        {
            SelectedRandomBonus = null;
            Dialog.Close();
        }
    }
}
